package com.example.tse.rbyair.base;

import android.animation.Animator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tse.rbyair.R;
import com.example.tse.rbyair.dialog.RbProgressDialog;

import org.kymjs.kjframe.base.BaseActivity;
import org.kymjs.kjframe.utils.DensityUtils;

import de.greenrobot.event.EventBus;

/**
 * Created by hp on 16/1/26.
 */
public abstract class RbBaseActivity extends BaseActivity{


    public LinearLayout content;
    private Dialog mLoading;
    private TextView back;
    public TextView titleTxt;
    public TextView titleRighttxt;
    public ImageView shopcart;
    public LinearLayout backLayout;
    private LinearLayout topLayout;
    private ImageView base_titleline;
    public  ImageView titleFilter;
    public TextView shopcart_point;
    public Context mContext;
    public RelativeLayout titleRightLayout;
    private TextView shopcart_pointTxt;
    private int b =0;
    public ImageView reclassify_addcartiv;
    public Animation mAnimation;
    private boolean isAniting = false;
    public final int MIN_CLICK_DELAY_TIME = 1000;
    private RbProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initBaseActivity();
//		getStaticsInfo();
    }

    public void getStaticsInfo(final int nn){
//        RemoteServiceFactory.getInstance().getShoppingService(this).getShoppingCartNum(new ShoppingGetNumRequest(), new RemoteServiceListener<ShoppingGetCartNumResponse>() {
//
//            @Override
//            public void ok(ShoppingGetCartNumResponse t) {
//                RbLog.d("lc","ShoppingGetCartNumResponse t =" + t.getCartNum());
//                if(nn==1){   //商品详情页
//                    if(t.getCartNum()>0){
//                        shopcart_point.setVisibility(0);
//                        if(t.getCartNum()>99){
//                            shopcart_point.setText("99+");
//                        }else{
//                            shopcart_point.setText(""+t.getCartNum());
//                        }
//                    }else{
//                        shopcart_point.setVisibility(8);
//                    }
//                }else{
//                    if(t.getCartNum()>0){
//                        shopcart_pointTxt.setVisibility(0);
//                        if(t.getCartNum()>99){
//                            shopcart_pointTxt.setText("99+");
//                        }else{
//                            shopcart_pointTxt.setText(""+t.getCartNum());
//                        }
//                    }else{
//                        shopcart_pointTxt.setVisibility(8);
//                    }
//
//                }
//            }
//
//            @Override
//            public void failue(int errorCode, String errorMessage) {
//
//            }
//        });
    }

    long lastClickTime = 0;

//    public void onEventMainThread(ShopCartNumEvent ne){
//        RbLog.i("ShopCartNumEvent ne"+ne.getState());
//        if (ne.getState().equals("base")) {
//            long currentTime = Calendar.getInstance().getTimeInMillis();
//            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
//                RbLog.i("lastClickTime="+lastClickTime+" currentTime="+currentTime);
//                lastClickTime = currentTime;
//                startAnimation(0);
//            }
//            getStaticsInfo(0);
//        }else if(ne.getState().equals("productdetial")){
////			startAnimation(1);
//            getStaticsInfo(1);
//        }
//
//    }

    private void initBaseActivity() {
        super.setContentView(R.layout.activity_base);
        reclassify_addcartiv = (ImageView)findViewById(R.id.reclassify_addcartiv);
        base_titleline = (ImageView) findViewById(R.id.base_titleline);
        content = (LinearLayout)findViewById(R.id.content);
        shopcart = (ImageView)findViewById(R.id.shopcart);
        base_titleline = (ImageView)findViewById(R.id.base_titleline);
        backLayout = (LinearLayout)findViewById(R.id.backLayout);
        topLayout = (LinearLayout)findViewById(R.id.topBaseLayout);
        titleRighttxt = (TextView)findViewById(R.id.titleRighttxt);
        back = (TextView)findViewById(R.id.topTitleLeftTxt);
        shopcart_point = (TextView)findViewById(R.id.shopcart_point);
        shopcart_pointTxt = (TextView)findViewById(R.id.shopcart_pointTxt);

        progressDialog = new RbProgressDialog();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        titleTxt = (TextView)findViewById(R.id.topTitleTxt);
        titleFilter = (ImageView)findViewById(R.id.filterImg);
        titleRightLayout = (RelativeLayout) findViewById(R.id.titleRightLayout);

        mAnimation = AnimationUtils.loadAnimation(this, R.anim.cart_anim);
        mAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                reclassify_addcartiv.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public void setContentView(int layoutResID) {
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(layoutResID, content);
    }

    private void startAnimation(int n){
        b = 0;
        if(n==0){
            titleFilter.animate().translationYBy(-DensityUtils.dip2px(this, 5)).setDuration(300).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {
                    b++;isAniting = true;
                }
                @Override
                public void onAnimationRepeat(Animator arg0) {}
                @Override
                public void onAnimationEnd(Animator arg0) {
                    isAniting = false;
                    if(b==1) titleFilter.animate().translationYBy(DensityUtils.dip2px(RbBaseActivity.this, 5)).setDuration(500).setInterpolator(new BounceInterpolator());
                }
                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
        }else{

            shopcart.animate().translationYBy(-DensityUtils.dip2px(this, 5)).setDuration(300).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {
                    b++;isAniting = true;
                }
                @Override
                public void onAnimationRepeat(Animator arg0) {}
                @Override
                public void onAnimationEnd(Animator arg0) {
                    isAniting = false;
                    if(b==1) shopcart.animate().translationYBy(DensityUtils.dip2px(RbBaseActivity.this, 5)).setDuration(500).setInterpolator(new BounceInterpolator());
                }
                @Override
                public void onAnimationCancel(Animator arg0) {}
            });
        }
    }

    public void setLeftTxt(int rsid){
        backLayout.setVisibility(View.VISIBLE);
        back.setText(getResources().getString(rsid));
    }

    public void setRightTxt(int rsid){
        titleRighttxt.setVisibility(View.VISIBLE);
        titleRighttxt.setText(getResources().getString(rsid));
    }

    public void hideTitle(){
        topLayout.setVisibility(View.GONE);
        base_titleline.setVisibility(View.GONE);
    }

    public void hideTitleInvisible(){
        topLayout.setVisibility(View.INVISIBLE);
    }
    public void hideLine() {
        base_titleline.setVisibility(View.GONE);
    }
    public void hideLeft(){
        backLayout.setVisibility(View.INVISIBLE);
    }

    public void setTitleTxt(int rsid){
        titleTxt.setText(getResources().getString(rsid));
    }

    public void setTitleTxt(String str){
        titleTxt.setText(str);
    }

    public void setRightImageResouse(int rsid){
        titleFilter.setImageResource(rsid);
    }

    public void hideRightImage(){
        titleFilter.setVisibility(View.INVISIBLE);
    }

    public void showShopCart(){
        shopcart.setVisibility(View.VISIBLE);
    }

    public void showShopCartPoint(){
        shopcart_point.setVisibility(View.VISIBLE);
    }

    public void hideShopCartPoint(){
        shopcart_point.setVisibility(View.GONE);
    }

    public void hidePoint(){
        shopcart_pointTxt.setVisibility(View.GONE);
    }

    public void showToast(String message) {
        Toast toast = Toast.makeText(this,message, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        HttpService.cancel(this);
    }

    public static int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }
    public static int px2dip(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }

    public int getScreenHeight(Context context)
    {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }


    public int getScreenWidth(Context context){
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

//    private OnCancelListener mOnCancelListener = new OnCancelListener() {
//
//        @Override
//        public void onCancel(DialogInterface arg0) {
////			finish();
////			overridePendingTransition(R.anim.slide_in_left,
////					R.anim.slide_out_right);
//        }
//    };

    /**
     * 显示加载对话框(正在加载...)
     */
    public void showLoadingDialog() {
        progressDialog.show(getSupportFragmentManager(), "");
    }

    public void dismissLoadingDialog() {
        progressDialog.dismiss();
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
            /*隐藏软键盘*/
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if(inputMethodManager.isActive()){
                inputMethodManager.hideSoftInputFromWindow(RbBaseActivity.this.getCurrentFocus().getWindowToken(), 0);
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}
